package com.x.mvp.appbar;

import android.view.LayoutInflater;
import android.view.View;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.x.mvp.R;


public class TabAppbarBuilder extends AppBarBuilder {
    SegmentTabLayout commonTabLayout;
    private String[] tabEntities;
    private OnTabSelectListener onTabSelectListener;
    private int rightTextMenu;
    private int leftTextMenu;

    @Override
    protected View getCenterSpace(LayoutInflater layoutInflater) {
        View centerView = layoutInflater.inflate(R.layout.tab_layout, null);
        commonTabLayout = (SegmentTabLayout) centerView.findViewById(R.id.tablayout);
        commonTabLayout.setTabData(tabEntities);

        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (onTabSelectListener != null) {
                    onTabSelectListener.onTabSelect(position);
                }
            }

            @Override
            public void onTabReselect(int position) {
                if (onTabSelectListener != null) {
                    onTabSelectListener.onTabReselect(position);
                }
            }
        });
        return centerView;
    }

    public void setCurrentTab(int pos) {
        commonTabLayout.setCurrentTab(pos);
    }

    @Override
    protected View getRightSpace(LayoutInflater layoutInflater) {
        if(rightTextMenu != 0) {
            return layoutInflater.inflate(rightTextMenu, null);
        }
        return null;
    }

    @Override
    protected View getLeftSpace(LayoutInflater layoutInflater) {
        if(leftTextMenu != 0) {
            return layoutInflater.inflate(leftTextMenu, null);
        }
        return null;
    }

    public TabAppbarBuilder setTabEntities(String[] tabEntities) {
        this.tabEntities = tabEntities;
        return this;
    }

    public TabAppbarBuilder setOnTabSelectListener(OnTabSelectListener onTabSelectListener) {
        this.onTabSelectListener = onTabSelectListener;
        return this;
    }

    public TabAppbarBuilder setRightTextMenu(int menuId) {
        this.rightTextMenu = menuId;
        return this;
    }

    public TabAppbarBuilder setLeftTextMenu(int leftTextMenu) {
        this.leftTextMenu = leftTextMenu;
        return this;
    }
}
